// pages/user/userCenter/comps/userInfo/userInfo.js
const md5 = require('../../../../../utils/md5.js')
const common = require('../../../../../utils/util.js')
Component({
    /**
     * 组件的属性列表
     */
    properties: {
        isShow: Boolean,
        userInfo: Object,
        unionID: Number,
        messageInfo: Object
    },

    /**
     * 组件的初始数据
     */
    data: {
        roleList: ['纹绣师', '形象代言人', '合伙人', '联合创始人']
    },

    /**
     * 组件的方法列表
     */
    methods: {
        objKeySort: function (obj) {
            let newkey = Object.keys(obj).sort()
            let newObj = {}
            for (let i = 0; i < newkey.length; i++) {
                newObj[newkey[i]] = obj[newkey[i]]
            }
            return newObj
        },
        goMessageCenter: function() {
            wx.navigateTo({
                url: '/pages/messageSystem/messageCenter/messageCenter',
            })
        },

        goAccount: function() {
            wx.navigateTo({
                url: '/pages/user/account/account'
            })
        },

        goIncome: function(e) {
            wx.navigateTo({
                url: '/pages/user/accumulatedIncome/accumulatedIncome?type=' + e.currentTarget.dataset.type
            })
        },

        goContribution: function() {
            wx.navigateTo({
                url: '/pages/user/contribution/contribution'
            })
        },

        goUserInfo: function() {
            // wx.navigateTo({
            //     url: '/pages/user/info/info'
            // })
        },

        showConcat: function() {
            this.triggerEvent('showConcat')
        },

        getUserInfo: function(e) {
            const that = this
            if (e.detail.errMsg == 'getUserInfo:ok') {
                wx.showLoading({
                    title: '登录中...',
                })
                wx.checkSession({
                    success() {
                        //session_key 未过期，并且在本生命周期一直有效
                        console.log('session_key 未过期，并且在本生命周期一直有效')
                        let data = {}
                        let key = wx.getStorageSync('token')
                        let sign = 'cds'
                        let timestamp = Date.parse(new Date())
                        data.key = key
                        data.timestamp = timestamp
                        data = that.objKeySort(data)
                        for (let i in data) {
                            sign += (i + data[i])
                        }
                        sign += 'cds'
                        sign = md5(sign)
                        data.sign = sign
                        data.code = wx.getStorageSync('code')
                        data.phone = wx.getStorageSync('userPhone')
                        data.wx_sm = 1
                        data.encryptedData = e.detail.encryptedData
                        data.iv = e.detail.iv
                        wx.request({
                            url: 'https://www.meiboshi.vip/api/Login/phone_unionid',
                            header: {
                                'content-type': 'application/x-www-form-urlencoded'
                            },
                            method: 'POST',
                            data: data,
                            success: function(res) {
                                wx.login({
                                    success(loginRes) {
                                        wx.setStorageSync('code', loginRes.code)
                                    }
                                })
                                const data = res.data.datas
                                if (res.data.code == 204) {
                                    wx.hideLoading()
                                    wx.showModal({
                                        title: '提示',
                                        content: '网络繁忙，请重新登录',
                                        showCancel: false,
                                        success(res) {}
                                    })
                                } else {
                                    wx.hideLoading()
                                    wx.setStorageSync('unionID', 1)
                                    common.refresh()
                                }
                            }
                        })
                    },
                    fail() {
                        // session_key 已经失效，需要重新执行登录流程
                        console.log('session_key 已经失效，需要重新执行登录流程')
                        wx.login({
                            success(res) {
                                if (res.code) {
                                    wx.setStorageSync('code', res.code)
                                    //发起网络请求
                                    let data = {}
                                    let key = wx.getStorageSync('token')
                                    let sign = 'cds'
                                    let timestamp = Date.parse(new Date())
                                    data.key = key
                                    data.timestamp = timestamp
                                    data = that.objKeySort(data)
                                    for (let i in data) {
                                        sign += (i + data[i])
                                    }
                                    sign += 'cds'
                                    sign = md5(sign)
                                    data.sign = sign
                                    data.code = res.code
                                    data.phone = wx.getStorageSync('userPhone')
                                    data.wx_sm = 1
                                    data.encryptedData = e.detail.encryptedData
                                    data.iv = e.detail.iv
                                    wx.request({
                                        url: 'https://www.meiboshi.vip/api/Login/phone_unionid',
                                        header: {
                                            'content-type': 'application/x-www-form-urlencoded'
                                        },
                                        method: 'POST',
                                        data: data,
                                        success: function(res) {
                                            wx.login({
                                                success(loginRes) {
                                                    wx.setStorageSync('code', loginRes.code)
                                                }
                                            })
                                            const data = res.data.datas
                                            if (res.data.code == 204) {
                                                wx.hideLoading()
                                                wx.showModal({
                                                    title: '提示',
                                                    content: '网络繁忙，请重新登录',
                                                    showCancel: false,
                                                    success(res) {}
                                                })
                                            } else {
                                                wx.hideLoading()
                                                wx.setStorageSync('isUserInfo', true)
                                                wx.setStorageSync('unionID', 1)
                                                common.refresh()
                                            }
                                        }
                                    })
                                }
                            }
                        })
                    }
                })
            }
        }
    }
})